package com.concurrent.coline.part7;

import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * 类说明：演示带版本戳的原子操作类
 */
public class UseAtomicStampedReference {

    private static AtomicStampedReference<String> asr =
            new AtomicStampedReference<>("Coline", 0);


    public static void main(String[] args) throws InterruptedException {
        //获取初始的版本号
        final int oldStamp = asr.getStamp();
        final String oldReferenc = asr.getReference();

        Thread rightStampThread = new Thread(() -> {

            System.out.println(Thread.currentThread().getName()
                    + "当前变量值：" + oldReferenc + "计划修改时版本戳：" + oldStamp + "-"
                    + asr.compareAndSet(oldReferenc, oldReferenc + "Java",
                    oldStamp, oldStamp + 1));


        });

        Thread errorStampThread = new Thread(() -> {

            System.out.println(Thread.currentThread().getName()
                    + "当前变量值：" + oldReferenc + "计划修改时版本戳：" + oldStamp + "-"
                    + asr.compareAndSet(oldReferenc, oldReferenc + "Java",
                    oldStamp, oldStamp + 1));


        });

        rightStampThread.start();
        rightStampThread.join();
        errorStampThread.start();
        errorStampThread.join();
        System.out.println(asr.getReference() + "===========" + asr.getStamp());

    }
}
